package com.itheima.health.config;


import com.itheima.health.job.QuartzDemo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

@Configuration
public class QuartzConfig {
    /**
     * 1、创建job对象
     * */
    @Bean
    public JobDetailFactoryBean jobDetailFactoryBean(){
        JobDetailFactoryBean factory = new JobDetailFactoryBean();
        //关联我们自己的Job类
        factory.setJobClass(QuartzDemo.class);
        return factory;
    }

    /**
     * 2、创建Trigger对象
     */
//    @Bean
//    public SimpleTriggerFactoryBean simpleTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
//        SimpleTriggerFactoryBean factory = new SimpleTriggerFactoryBean();
//        //关联JobDetail对象
//        factory.setJobDetail(jobDetailFactoryBean.getObject());
//        //设置触发时间毫秒数
//        factory.setRepeatInterval(5000);
//        //重复次数
//        factory.setRepeatCount(5);
//        return factory;
//    }

    @Bean
    public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean) {
        CronTriggerFactoryBean factory = new CronTriggerFactoryBean();
        factory.setJobDetail(jobDetailFactoryBean.getObject());
        //设置触发时间
        factory.setCronExpression("*/15 * * * * ?");//每三秒执行一次
        //factory.setCronExpression("0 0 0 L 1 ? ");  //本月最后一天
        return factory;
    }

    /**
     * 3、创建Scheduler对象
     */
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean, MyAdaptableJobFactory myAdaptableJobFactory){
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        //关联trigger
        factory.setTriggers(cronTriggerFactoryBean.getObject());
        //重新设置将Job对象通过spring初始化，然后添加到springioc容器
        factory.setJobFactory(myAdaptableJobFactory);
        return factory;
    }
}
